require.config({
    paths: {
        'jquery': ['http://cdn.bootcss.com/jquery/2.2.4/jquery.min', '/js/lib/jquery.min'],
        'doT': '/js/lib/doT.min',
        'bootstrap': '/js/lib/bootstrap.min',
        'paginator': '/js/lib/bootstrap-paginator'
    },
    shim: {
        'bootstrap': ['jquery'],
        'paginator': ['bootstrap']
    }
});

var myFun = {};

require(['jquery', 'doT', 'common', 'paginator', 'bootstrap'], function($, doT, common, paginator) {
    $(function() {
        var role = sessionStorage.getItem('role');
        var pageSize = 10;
        $(document).ajaxStart(function() {
            $('#tips').show();
        }).ajaxStop(function() {
            $('#tips').hide();
        });



        function refreshMaster(page) {
            getMaster(page, pageSize);
        }
        refreshMaster(1);

        function getMaster(page, pageSize) {
            var data = {
                'role': 2,
                'page': page,
                'pageSize': pageSize
            }

            $.get(common.urlBase + '/school/allSchool.json', function(data, statu) {
                      data = eval('(' + data + ')');
                   myFun.schoolList = data.data;

            });
            $.get(common.urlBase + '/account/get.json?role=2', function(data, statu) {
                var data = eval('(' + data + ')');
                if (data['ok']) {
                    masterList = data.data.obj;
                    var total = data.data.count;
                    var pageNumber = Math.ceil(total / pageSize);

                    $.each(masterList, function(index, value) {
                        var id = this.school_id;
                        $.each(myFun.schoolList, function() {
                            if (this.id == id) {
                                masterList[index].school_name = this.name;
                            }
                        });

                    });

                    var masterTmpl = doT.template($('#template_master').text());
                    $('#master').html(masterTmpl(masterList));

                    $('#total').text(total);
                    initPaginator(page, 8, pageNumber);

                    if (role == 1) {
                        $('#add_master').removeClass('managerOnly');
                        $('th').removeClass('managerOnly');
                        $('td').removeClass('managerOnly');
                    }
                } else {
                    $('#noticeMsg').text(data['message']);
                    $('#noticeModal').modal('show');
                }
            });
        }


        function initPaginator(currentPage, numberOfPages, totalPages) {
            var options = {
                currentPage: currentPage,
                numberOfPages: numberOfPages,
                totalPages: totalPages,
                itemTexts: function(type, page, current) {
                    switch (type) {
                        case 'first':
                            return '首页';
                        case 'prev':
                            return '上一页';
                        case 'next':
                            return '下一页';
                        case 'last':
                            return '末页';
                        case 'page':
                            return page;

                    }
                },
                onPageChanged: function(event, oldPage, newPage) {
                    refreshMaster(newPage);
                }
            };
            $('#paginator').bootstrapPaginator(options);
        }
        myFun.deleteMaster = function deleteMaster(obj) {
            var id = $(obj).data('target');
            if (confirm('是否要删除？')) {
                $.get(common.urlBase + '/account/delete/' + id + '.json', function(data, statu) {
                    if (statu == 'success') {
                        location.reload();
                    } else {
                        $('#noticeMsg').text('删除失败！');
                        $('#noticeModal').modal('show');
                    }
                });
            }
        }

        myFun.changeMaster = function changeMaster(obj) {
            var id = $(obj).data('target');
            var data = 'id=' + id;
            $(obj).attr('href', '/views/change_master.html?' + data);
        }

    });
});
